﻿#include "dropdownlistpanel.h"
#include "nodemanager/wnodemanager.h"

wxString LabeledComboBox::getParams(void)
{
    WBaseNode *pBaseNode = WNodeManager::getSingleton().getNodeByName(m_label->GetLabel());
    int decNodeId = -1;

    if(pBaseNode == NULL)
    {
        tagSectNode *pSectNode = WNodeManager::getSingleton().getSect(m_label->GetLabel());
        if(pSectNode != NULL)
            decNodeId = pSectNode->deviceID;
    }
    else
    {
        decNodeId = pBaseNode->getDeviceID();
    }

    return wxString::Format("{\"%d\":%d}",
                            decNodeId,
                            m_combo->GetSelection());
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DropDownListPanel::DropDownListPanel(wxWindow* parent,
                                     wxString typeName,
                                     wxWindowID id,
                                     const wxPoint& pos,
                                     const wxSize& size)
    : wxScrolledWindow(parent, id, pos, size, wxVSCROLL),
      m_typeName(typeName)
{
    SetScrollRate(0, 20); // 设置垂直滚动步长

    // 设置最小大小以确保滚动条正确计算
    SetMinSize(parent->GetScreenRect().GetSize());

    m_mainSizer = new wxBoxSizer(wxVERTICAL);
    SetSizer(m_mainSizer);
}

DropDownListPanel::~DropDownListPanel()
{

}

void DropDownListPanel::FitInside() {
    wxScrolledWindow::FitInside();
    AdjustScrollbars();
}

void DropDownListPanel::setParams(wxString node,wxString state)
{
    if(node == "" || state == "")
        return;

    for(int i=0;i<m_listItems.size();i++)
    {
        for(int k=0;k<m_listItems[i].items.size();k++)
        {
            if(m_listItems[i].items[k]->GetName() != node)
                continue;

            m_listItems[i].items[k]->GetComboBox()->SetValue(state);
            break;
        }
    }
}

wxString DropDownListPanel::getParams(wxVector<wxString> nodes)
{
    wxString resultString/*=wxString::Format("\"%s\":[",m_typeName)*/;
    bool isFinded = false;

    for(int i=0;i<m_listItems.size();i++)
    {
        for(int k=0;k<m_listItems[i].items.size();k++)
        {
            if(!nodes.empty())
            {
                wxVector<wxString>::iterator iter = std::find(nodes.begin(),
                                                              nodes.end(),
                                                              m_listItems[i].items[k]->GetName());
                if(iter != nodes.end())
                {
                    isFinded = true;
                    resultString+=(m_listItems[i].items[k]->getParams()+",");
                }
            }
            else
            {
                isFinded = true;
                resultString+=(m_listItems[i].items[k]->getParams()+",");
            }
        }
    }

    if(isFinded && !resultString.IsEmpty())
        resultString = resultString.Mid(0,resultString.Length()-1);

    //resultString+="]";

    return resultString;
}

/// 添加一个选项到列表中
void DropDownListPanel::addLabeledComboBox(LabeledComboBox *lc)
{
    if(lc == NULL) return;

    lc->setTypeName(this->m_typeName);

    if(m_listItems.empty())
    {
        tagListItem ptagListItem;
        ptagListItem.boxsizer = new wxBoxSizer(wxHORIZONTAL);
        ptagListItem.boxsizer->Add(lc, 0, wxEXPAND | wxRIGHT, 10);
        ptagListItem.items.push_back(lc);

        m_mainSizer->Add(ptagListItem.boxsizer,0,wxEXPAND);

        m_listItems.push_back(ptagListItem);
    }
    else
    {
        if(m_listItems[m_listItems.size()-1].items.size() < 6)
        {
           m_listItems[m_listItems.size()-1].boxsizer->Add(lc, 0, wxEXPAND | wxRIGHT, 10);
           m_listItems[m_listItems.size()-1].items.push_back(lc);
        }
        else
        {
            tagListItem ptagListItem;
            ptagListItem.boxsizer = new wxBoxSizer(wxHORIZONTAL);
            ptagListItem.boxsizer->Add(lc, 0, wxEXPAND | wxRIGHT, 10);
            ptagListItem.items.push_back(lc);

            m_mainSizer->Add(ptagListItem.boxsizer,0,wxEXPAND);

            m_listItems.push_back(ptagListItem);
        }
    }

    // 更新布局和滚动条
    Layout();
    FitInside();
}
